package com.lwg.datastructure.dynamicPrograming;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TargetSum1 {

    public int findTargetSumWays(int[] nums, int target) {
        int length = nums.length;
        int sum = 0;
        //该数组所有数的和  capacity=(sum-target)/2
        for (int num : nums) {
            sum += num;
        }
        //capacity不能小于0  不能为奇数
        if (sum < target || ((sum - target) & 1) != 0) {
            return 0;
        }
        int capacity = (sum - target) >> 1;
        //初始化数组 int[3] 下标只有0,1,2
        int dp[][] = new int[length + 1][capacity + 1];

        //当不选择数时 和为0的只有一种
        dp[0][0] = 1;
        //从选一个数开始
        for (int i = 1; i <= length; i++) {
            for (int j = 0; j <= capacity; j++) {
                //如何当前数字小于或等于j时 可选可不选
                if (j >= nums[i - 1]) {
                    dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i - 1]];
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }

        }
        return dp[length][capacity];

    }
}

class MainClass1 {
    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
            return new int[0];
        }

        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for (int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            int[] nums = stringToIntegerArray(line);
            line = in.readLine();
            int target = Integer.parseInt(line);

            int ret = new TargetSum1().findTargetSumWays(nums, target);

            String out = String.valueOf(ret);

            System.out.print(out);
        }
    }
}
